
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CSharPro._1106
{
    public class UserService : IUserService
    {
        // 泛型集合存储用户信息
        private readonly List<User> _users = new List<User>();
        public void Start()
        {
            ShowMainMenu();
        }
        private void ShowMainMenu()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("===== 用户系统 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请选择功能(1-3): ");

                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        Login();
                        break;
                    case "2":
                        Register();
                        break;
                    case "3":
                        Console.WriteLine("谢谢使用，再见！");
                        return;
                    default:
                        Console.WriteLine("输入错误，请输入1-3之间的数字！");
                        WaitForKey();
                        break;
                }
            }
        }
        
        public bool Register()
        {
            Console.Clear();
            Console.WriteLine("===== 用户注册 =====");

            // 获取并验证用户名
            string username = GetValidUsername();
            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("注册取消！");
                WaitForKey();
                return false;
            }

            // 获取并验证密码
            string password = GetValidPassword();
            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("注册取消！");
                WaitForKey();
                return false;
            }

            // 确认密码
            string confirmPassword;
            do
            {
                Console.Write("请输入确认密码: ");
                confirmPassword = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(confirmPassword))
                {
                    Console.WriteLine("确认密码不可为空！");
                    continue;
                }

                if (confirmPassword != password)
                {
                    Console.WriteLine("两次输入的密码不一致，请重新输入！");
                }
            } while (confirmPassword != password);

            // 添加用户到泛型集合
            _users.Add(new User(username, password));
            Console.WriteLine("注册成功！");
            WaitForKey();
            return true;
        }

        // 验证合法性
        private string GetValidUsername()
        {
            while (true)
            {
                Console.Write("请输入用户名: ");
                string username = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(username))
                {
                    Console.WriteLine("用户名不可为空！");
                    continue;
                }

                if (username.Length < 3 || username.Length > 15)
                {
                    Console.WriteLine("用户名长度必须为3-15个字符！");
                    continue;
                }

                if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
                {
                    Console.WriteLine("用户名只能包含字母、数字和下划线！");
                    continue;
                }

                // 使用foreach循环检查用户名是否已存在
                bool isExists = false;
                foreach (var user in _users)
                {
                    if (user.Username == username)
                    {
                        isExists = true;
                        break;
                    }
                }

                if (isExists)
                {
                    Console.WriteLine("用户名已存在，请更换！");
                    continue;
                }

                return username;
            }
        }

        
        private string GetValidPassword()
        {
            while (true)
            {
                Console.Write("请输入密码: ");
                string password = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(password))
                {
                    Console.WriteLine("密码不可为空！");
                    continue;
                }

                if (password.Length < 6 || password.Length > 20)
                {
                    Console.WriteLine("密码长度必须为6-20个字符！");
                    continue;
                }

                bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
                bool hasLower = Regex.IsMatch(password, @"[a-z]");
                bool hasNumber = Regex.IsMatch(password, @"[0-9]");

                if (!hasUpper || !hasLower || !hasNumber)
                {
                    Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字！");
                    continue;
                }

                return password;
            }
        }

       
        public bool Login()
        {
            Console.Clear();
            Console.WriteLine("===== 用户登录 =====");

            Console.Write("请输入用户名: ");
            string username = Console.ReadLine();

            Console.Write("请输入密码: ");
            string password = Console.ReadLine();

            // 使用foreach循环查找用户
            User foundUser = null;
            foreach (var user in _users)
            {
                if (user.Username == username)
                {
                    foundUser = user;
                    break;
                }
            }

            if (foundUser == null)
            {
                Console.WriteLine("登录失败：用户名不存在！");
                WaitForKey();
                return false;
            }

            if (foundUser.Password != password)
            {
                Console.WriteLine("登录失败：密码错误！");
                WaitForKey();
                return false;
            }

            Console.WriteLine($"登录成功！欢迎回来，{username}！");
            Console.WriteLine("已进入系统首页...");
            Console.WriteLine("按任意键返回主菜单...");
            Console.ReadKey();
            return true;
        }

       
        public bool DeleteUser(string username)
        {
            for (int i = 0; i < _users.Count; i++)
            {
                if (_users[i].Username == username)
                {
                    _users.RemoveAt(i);
                    return true;
                }
            }
            return false;
        }

        
        private void WaitForKey()
        {
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
        }
    }
}